package org.chatboleh.util;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.chatboleh.bean.TextElement;

public class PrintableDocument implements Printable {
	private Logger logger = Logger.getLogger(this.getClass());
	private List<TextElement> textelementlist;
	private Rectangle imageableArea;

	public PrintableDocument() {
		textelementlist = new ArrayList<TextElement>();
		imageableArea = new Rectangle(0, 0, 400, 200);
		logger.debug("Calling constructor.");
	}

	@Override
	public int print(Graphics g, PageFormat pf, int page)
			throws PrinterException {
		logger.debug("Printing page: " + page);
		logger.debug("Old PageWidth = " + pf.getWidth());
		logger.debug("Old PageHeight = " + pf.getHeight());
		Paper paper = new Paper();
		paper.setImageableArea( 0, 0, 7 * 72, 3.5 * 72 );
//		paper.setSize( 7 * 72, 3.5 * 72 );
		pf.setPaper(paper);

		logger.debug("New PageWidth = " + pf.getWidth());
		logger.debug("New PageHeight = " + pf.getHeight());
		if (page > 0) { /* We have only one page, and 'page' is zero-based */
			return NO_SUCH_PAGE;
		}

		// Set Printable area
		// Paper paper = new Paper();
		// paper.setImageableArea(this.imageableArea.getX(), this.imageableArea
		// .getY(), this.imageableArea.getWidth(), this.imageableArea
		// .getHeight());
		// pf.setPaper(paper);
		// pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
		/*
		 * User (0,0) is typically outside the imageable area, so we must
		 * translate by the X and Y values in the PageFormat to avoid clipping
		 */
		Graphics2D g2d = (Graphics2D) g;
		g2d.translate(pf.getImageableX(), pf.getImageableY());
		
		/* Now we perform our rendering */

		// remove this later
		logger.debug("Drawing Rect.");
//		g2d.setColor(Color.yellow);
//		g2d.fillRect((int) pf.getImageableX(), (int) pf.getImageableY(),
//				(int) pf.getImageableWidth(), (int) pf.getImageableHeight());
//		g2d.setColor(Color.black);
		g2d.drawRect((int) this.imageableArea.getX(), (int) this.imageableArea
				.getY(), (int) this.imageableArea.getWidth(),
				(int) this.imageableArea.getHeight());
		for (int i = 0; i < textelementlist.size(); i++) {
			TextElement te = textelementlist.get(i);
			logger.debug("Printing > " + te.getText());
			g2d.setColor(te.getFontColor());
			g2d.setFont(new Font(te.getFontName(), te.getFontStyle(), te
					.getFontSize()));
			g2d.drawString(te.getText(), te.getX(), te.getY());

		}
		logger.debug("Finish Printing\n.");
		/* tell the caller that this page is part of the printed document */
		return PAGE_EXISTS;

	}

	public List<TextElement> getTextelementlist() {
		return textelementlist;
	}

	public void setTextelementlist(List<TextElement> textelementlist) {
		this.textelementlist = textelementlist;
	}

	public void addTextElement(TextElement textelement) {
		logger.debug("TextElementList Size = " + this.textelementlist.size());
		logger.debug("Adding TextElement: " + textelement.getText());
		this.textelementlist.add(textelement);
	}

	public Rectangle getImageableArea() {
		return imageableArea;
	}

	public void setImageableArea(Rectangle imageableArea) {
		this.imageableArea = imageableArea;
	}

}
